/*
Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
*/

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        // if (!m && !n) return;
        int idx_a = m-1, idx_b = n-1;
        int idx_m = m+n-1; //merged index;
        while (idx_a >= 0 && idx_b >= 0) {
            if (A[idx_a] < B[idx_b]) A[idx_m--] = B[idx_b--];
            else A[idx_m--] = A[idx_a--];
        }
        while (idx_b >= 0) A[idx_m--] = B[idx_b--];
    }
};
